﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;

namespace PSDManager
{
    public partial class TaskInfoPanel : UserControl, IDataPanel
    {
        public event EventHandler OnDownloadStateChanged;

        private ManagerHelper managerHelper;
        public ManagerHelper ManagerHelper
        {
            get
            {
                return managerHelper;
            }
            set
            {
                managerHelper = value;
                if (taskInfo1 != null)
                {
                    taskInfo1.ManagerHelper = managerHelper;
                }
            }
        }
        public dl_task Task { get; private set; }
        public List<dl_task_channel> Channels { get; private set; }
        public TaskInfoPanel()
        {
            InitializeComponent();

            chkEndTime.Checked = false;
            dtpEndTime.Enabled = false;

            taskInfo1.OnDownloadStateChanged += TaskInfo1_OnDownloadStateChanged;
        }

        private void TaskInfo1_OnDownloadStateChanged(object sender, EventArgs e)
        {
            OnDownloadStateChanged?.Invoke(sender, e);
        }

        public void Reset(bool immediate = true)
        {
            dtpStartTime.Value = DateTime.Now;
            dtpEndTime.Value = DateTime.Now;
            chkEndTime.Checked = false;

            taskInfo1.Reset();
        }

        public void RefreshData(params object[] parameter)
        {
            if (parameter.Length > 0)
            {
                Task = (dl_task)parameter[0];
            }
            else
            {
                Reset(true);
                return;
            }

            var dt = ManagerHelper.DownloadTaskList.Where(t => t.Task.id == Task.id).FirstOrDefault();

            if (dt != null)
            {
                Channels = dt.ChannelList;
            }

            dtpStartTime.Value = Task.start_time;
            if (Task.end_time != null)
            {
                chkEndTime.Checked = true;
                dtpEndTime.Enabled = true;
                dtpEndTime.Value = Task.end_time.Value;
            }
            else
            {
                chkEndTime.Checked = false;
                dtpEndTime.Enabled = false;
                dtpEndTime.Value = DateTime.Now;
            }

            taskInfo1.SetTaskInfo(Task, Channels);
        }

        private void btnEditTaskTime_Click(object sender, EventArgs e)
        {
            if (Task != null)
            {
                DateTime st = dtpStartTime.Value;
                DateTime? et = null;

                if (chkEndTime.Checked)
                {
                    et = dtpEndTime.Value;
                }

                var ret = ManagerHelper.SetTaskTime(Task.id, st, et);

                if (ret.success)
                {
                    MessageBox.Show("下载时间范围已修改！");
                }
                else
                {
                    MessageBox.Show("下载时间范围修改失败！");
                }
            }
        }

        private void chkEndTime_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndTime.Enabled = chkEndTime.Checked;
        }
    }
}
